package com.changgou.item.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.item.service.PageService;
import org.assertj.core.util.Arrays;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.thymeleaf.context.Context;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;

@Controller
@RequestMapping("/page")
public class PageController {

    @Autowired
    private PageService pageService;

    @Autowired
    protected HttpServletResponse response;
    /**
     * 生成静态页面
     *
     * @param id
     * @return
     */
    @RequestMapping("/createHtml/{id}")
    @ResponseBody
    public Result createHtml(@PathVariable(name = "id") Long id) {
        pageService.createPageHtml(id);
        return new Result(true, StatusCode.OK, "ok");

    }


    @RequestMapping("/getData/{id}")
    @ResponseBody
    public Result getData(@PathVariable(name = "id") Long id) {
        Map<String, Object> data = pageService.getData(id);
        return new Result(true, StatusCode.OK, "ok", data);
    }


    @RequestMapping("/getHtml/{id}")
    public void getHtml(@PathVariable(name = "id") Long id) {
        String html = pageService.getHtml(id);

        try {

            ServletOutputStream outputStream = response.getOutputStream();
            outputStream.write(html.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


}